<!-- 项目编辑弹窗 -->
<template>
  <el-dialog
    :title="isUpdate ? '修改落地页模板' : '添加落地页模板'"
    :visible="visible"
    width="1000px"
    :destroy-on-close="true"
    :lock-scroll="false"
    @update:visible="updateVisible"
    :close-on-click-modal="false"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-form-item label="模板名称:" prop="name">
        <el-input
          v-model="form.name"
          :placeholder="'请输入落地页模板名称'"
        ></el-input>
      </el-form-item>
      <el-form-item label="上传模板包:" prop="template_file">
        <el-upload
          accept=".zip"
          class="upload-demo"
          :action="uploadImgUrl"
          :on-preview="handlePreview"
          :on-remove="handleRemove"
          :on-success="successUpload"
          :before-remove="beforeRemove"
          multiple
          :limit="1"
          :headers="myHeaders"
          :on-exceed="handleExceed"
          :file-list="fileList"
        >
          <div>
            <el-button size="small" type="primary">点击上传</el-button>
            <div>（不可上传同名文件）</div>
          </div>
        </el-upload>
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button @click="updateVisible(false)">取消</el-button>
      <el-button type="primary" @click="save" :loading="loading"
        >保存
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import setting from "@/config/setting";
export default {
  name: "AppEdit",
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object
  },

  data() {
    let token = setting.takeToken();
    return {
      // 表单数据
      form: Object.assign({}, this.data),
      // 表单验证规则
      rules: {
        name: [{ required: true, message: "请输入模板名称", trigger: "blur" }],
        template_file: [
          { required: true, message: "请上传模板包", trigger: "blur" }
        ]
      },
      // 提交状态
      loading: false,
      // 导入
      fileList: [],
      uploadImgUrl:
        process.env.VUE_APP_API_BASE_URL + "Packagelandingtemplate/uploadFile",
      myHeaders: { Authorization: token },
      // 是否是修改
      isUpdate: false,
      filePath: ""
    };
  },
  watch: {
    data() {
      if (this.data) {
        this.form = Object.assign({}, this.data);
        this.isUpdate = true;
        this.rules.template_file = [
          { required: false, message: "请上传模板包", trigger: "blur" }
        ];
      } else {
        this.form = {};
        this.isUpdate = false;
        this.rules.template_file = [
          { required: true, message: "请上传模板包", trigger: "blur" }
        ];
      }
    }
  },
  methods: {
    // 导入
    handlePreview(file) {
      console.log(file);
    },
    handleRemove() {
      this.fileList = [];
      this.filePath = "";
      this.rules.template_file = [
        { required: true, message: "请上传模板包", trigger: "blur" }
      ];
    },
    successUpload(response) {
      if (response.code == 0) {
        this.$message.success(response.msg);
        this.filePath = response.data.path;
        console.log("成功时候的path", response.data.path, this.filePath);
        this.rules.template_file = [
          { required: false, message: "请上传模板包", trigger: "blur" }
        ];
      } else {
        this.$message.error(response.msg);
        this.rules.template_file = [
          { required: true, message: "请上传模板包", trigger: "blur" }
        ];
      }
      // console.log("response", response, "this.fileList", this.fileList);
    },
    beforeRemove(file) {
      return this.$confirm(`确定移除 ${file.name}？`);
    },
    handleExceed() {
      this.$message.warning(`当前限制选择 1 个文件`);
    },
    /* 保存编辑 */
    save() {
      let params={
        name:this.form.name,
        path:this.filePath?this.filePath:this.form.path
      }
      if(this.isUpdate){
        params.id=this.form.id
      }
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.loading = true;
          this.$http
            .post("/Packagelandingtemplate/edit", params)
            .then(res => {
              this.loading = false;
              if (res.data.code === 0) {
                this.$message.success(res.data.msg);
                if (!this.isUpdate) {
                  this.form = {};
                }
                this.updateVisible(false);
                this.$emit("done");
              } else {
                this.fileList = [];
                this.handleRemove()
                this.$message.error(res.data.msg);
              }
            })
            .catch(e => {
              this.fileList = [];
              this.handleRemove()
              this.loading = false;
              this.$message.error(e.message);
            });
        } else {
          return false;
        }
      });
    },
    /* 更新visible */
    updateVisible(value) {
      this.$emit("update:visible", value);
      this.rules.template_file = [
        { required: true, message: "请上传模板包", trigger: "blur" }
      ];
      this.filePath = "";
    }
  }
};
</script>

<style scoped></style>
